'''urllib_爬取豆瓣网数据案例1'''

import urllib.request
import urllib.parse
import json

# 爬取豆瓣网影视地址
douban_url = 'https://movie.douban.com/j/chart/top_list?type=5&interval_id=100%3A90&action=&start=0&limit=20'

# 定义请求头
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"
}

# GET请求数据
request = urllib.request.Request(url=douban_url, headers=headers)
response = urllib.request.urlopen(request)
content = response.read().decode('utf-8')
print(content)

# # 将获取到的数据 写入文件中；
# fp = open('D:\\hxy\\test\\豆瓣网爬取的数据.json', 'w', encoding='utf-8')
# fp.write(content)
# fp.close()

# 将获取到的数据 写入文件中； 👍
with open('D:\\hxy\\test\\豆瓣网爬取的数据.json', 'w', encoding='utf-8') as fp:
    fp.write(content)

# 将字符串转json
json_content = json.loads(content)
print(type(json_content), json_content)
for item in json_content:
    print(item.get('cover_url'))


